﻿using System;
using System.Collections.Generic;

namespace YArchitech.Plumbing
{
	internal class HYSupplyCal
	{
		public void CalSystem(HYSystemCal sys, bool bIsDesign)
		{
			this.systemCal = sys;
			this.pipeCals = this.systemCal.PipeCals;
			this.bIsDesCal = bIsDesign;
			for (int i = 0; i < this.pipeCals.Count; i++)
			{
				HYPipeCal hypipeCal = this.pipeCals[i];
				this.CalPipe(ref hypipeCal);
			}
		}

		public bool GetRatedFlow(HYBathroom bathRoom, ref double dRatedFlow)
		{
			dRatedFlow = bathRoom.dSupplyFlow;
			return true;
		}

		public bool GetRatedEquivalent(HYBathroom bathRoom, ref double dEquivalent)
		{
			dEquivalent = bathRoom.dSupplyEquivalent;
			return true;
		}

		public bool GetUserPer(HYBathroom bathRoom, ref double dPer)
		{
			dPer = bathRoom.dPer;
			return true;
		}

		public double CalUoCoefficient(HYPipeCal pipe)
		{
			double num = 0.0;
			int num2 = 0;
			this.GetPipeEqAndFloors(pipe, ref num, ref num2);
			if (num2 < 1)
			{
				num2 = 1;
			}
			double num3 = num / (double)num2;
			return this.supplyFormula.dWaterNum * this.supplyFormula.nPersonNum * this.supplyFormula.dHourCon / (0.2 * this.supplyFormula.dEquivalent * this.supplyFormula.dUserTime * 3600.0) * 100.0;
		}

		public void GetPipeEqAndFloors(HYPipeCal pipe, ref double dEq, ref int numFloors)
		{
			if (pipe.endBathroom != null)
			{
				dEq += pipe.endBathroom.dSupplyEquivalent;
			}
			if (!pipe.Number.Contains("R"))
			{
				foreach (HYPipeCal hypipeCal in this.systemCal.MainPipeCals)
				{
					if (hypipeCal.bIsHorL && hypipeCal == pipe)
					{
						numFloors++;
					}
				}
			}
			foreach (HYPipeCal pipe2 in pipe.subPipeArray)
			{
				this.GetPipeEqAndFloors(pipe2, ref dEq, ref numFloors);
			}
		}

		public bool GetACoefficient(double dUo, ref double dA)
		{
			Dictionary<double, double> supplyACoefficient = HYCalSQLData.GetSupplyACoefficient();
			if (supplyACoefficient.Count < 1)
			{
				return false;
			}
			double num = 0.0;
			double num2 = 0.0;
			int num3 = 0;
			foreach (KeyValuePair<double, double> keyValuePair in supplyACoefficient)
			{
				if (num3 == 0)
				{
					if (dUo <= keyValuePair.Key)
					{
						dA = keyValuePair.Value;
						return true;
					}
					num = keyValuePair.Key;
					num2 = keyValuePair.Value;
				}
				if (dUo < keyValuePair.Key)
				{
					double num4 = (keyValuePair.Key - num) / (keyValuePair.Value - num2);
					dA = num2 + num4 * (dUo - num);
					return true;
				}
				num = keyValuePair.Key;
				num2 = keyValuePair.Value;
			}
			dA = num2;
			return true;
		}

		public int GetDNFromFlow(HYPipeCal pipe)
		{
			int num = 15;
			foreach (KeyValuePair<int, double> keyValuePair in HYCalSQLData.GetSupplyEconomicalFlow())
			{
				foreach (KeyValuePair<int, double> keyValuePair2 in pipe.dirDNArray)
				{
					num = keyValuePair2.Key;
					double value = keyValuePair2.Value;
					if (num > keyValuePair.Key)
					{
						break;
					}
					double num2 = 0.78539816339744828 * Math.Pow(value * 0.001, 2.0);
					if (0.001 * pipe.Flow / num2 <= keyValuePair.Value)
					{
						return num;
					}
				}
			}
			return num;
		}

		public bool CalPipe(ref HYPipeCal pipe)
		{
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			double num4 = 0.0;
			double num5 = 0.0;
			double num6 = 0.0;
			bool flag = false;
			foreach (HYBathroom hybathroom in pipe.upBathroomArray)
			{
				this.GetRatedFlow(hybathroom, ref num);
				if (num3 < num)
				{
					num3 = num;
				}
				num2 += num * (double)hybathroom.nCount;
				if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT || this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_ZZ)
				{
					this.GetRatedEquivalent(hybathroom, ref num4);
					if (hybathroom.szName.Contains("自闭"))
					{
						if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT)
						{
							num4 = 0.5;
						}
						flag = true;
					}
					num5 += num4 * (double)hybathroom.nCount;
				}
				else if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_PER)
				{
					this.GetUserPer(hybathroom, ref num6);
					this.GetRatedFlow(hybathroom, ref num);
					double num7 = num * (num6 / 100.0) * (double)hybathroom.nCount;
					if (hybathroom.szName.Contains("自闭") && num7 < 1.2)
					{
						num7 = 1.2;
					}
					num5 += num7;
				}
			}
			double num8 = 0.0;
			if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT)
			{
				num8 = 0.2 * this.supplyFormula.a * Math.Sqrt(num5);
				if (flag)
				{
					num8 += 1.2;
				}
				if (num8 < num3)
				{
					num8 = num3;
				}
				if (num8 > num2)
				{
					num8 = num2;
				}
			}
			else if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_PER)
			{
				if (num5 > num3)
				{
					num8 = num5;
				}
				else
				{
					num8 = num3;
				}
			}
			else if (this.supplyFormula.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_ZZ)
			{
				double num9 = 0.0;
				this.GetACoefficient(this.CalUoCoefficient(pipe), ref num9);
				if (num5 < 1.0)
				{
					num8 = 1.0;
				}
				else
				{
					num8 = (1.0 + num9 * Math.Pow(num5 - 1.0, 0.49)) / Math.Sqrt(num5);
				}
				num8 = 0.2 * num8 * num5;
			}
			pipe.Flow = num8;
			int nDN;
			if (!this.bIsDesCal)
			{
				nDN = pipe.nDN;
			}
			else
			{
				nDN = this.GetDNFromFlow(pipe);
			}
			double velocity = 0.0;
			double num10 = 0.0;
			double inDN = pipe.GetInDN();
			bool flag2;
			if (this.bIsCoolWater)
			{
				double y = 1.31E-06;
				flag2 = HYPlumbBaseCal.HZWLYL_Get_VAndI(num8 / 1000.0, inDN / 1000.0, y, this.calParameter.c, ref velocity, ref num10);
			}
			else
			{
				flag2 = HYPlumbBaseCal.MLYL_Get_VAndI(num8 / 1000.0, inDN, this.calParameter.n, ref velocity, ref num10);
			}
			if (flag2)
			{
				pipe.Velocity = velocity;
				pipe.dLostWator = num10;
				pipe.LossPipe = num10 * pipe.Length;
				pipe.LossPart = pipe.LossPipe * this.dQuotLoss;
				pipe.LossAll = pipe.LossPipe + pipe.LossPart;
				pipe.nDN = nDN;
			}
			num5 = 0.0;
			return true;
		}

		private HYSystemCal systemCal;

		private List<HYPipeCal> pipeCals;

		private bool bIsDesCal = true;

		public bool bIsCoolWater = true;

		public double dQuotLoss = 0.3;

		public HYCalParameter calParameter = new HYCalParameter();

		public HYSupplyFormula supplyFormula = new HYSupplyFormula();
	}
}
